<?
	/**
	* File: validateInput
	* Description: library of generic validation functions
	*/


	/**
	* Function: validateHTML
	* Input:	STRING data, REFERENCE description
	* Output: BOOLEAN
	* Description: Returns TRUE if data is composed of printable characters only.
	* $description set with appropriate error message.
	*/
	function validateHTML($strData, &$strDescription)
	{
		if(eregi('[^[:print:][:space:]]', $strData))
		{
			$strDescription = "Please compose the text for $strDescription <br/>using letters, numbers, spaces, and the basic punctuation marks <br/>( , . ? ! - [ ] / & ; ( )  ' \" : + * % $ @ ).<br/>";
			return(FALSE);
		}

		if(strip_tags($strData, '<br><b><i><p><u><a>') != $strData)
		{
			$strDescription = "Please use the following HTML only : <br/>&lt;br&gt;&lt;b&gt;&lt;i&gt;&lt;p&gt;&lt;u&gt;&lt;a&gt; for $strDescription field<br/>";
			return(FALSE);
		}

		return(TRUE);
	}

	/** Function: validateEmail
	* Input: STRING data, REFERENCE description
	* Output: BOOLEAN
	* Description: Returns TRUE if data looks like an email address.
	* $strDescription set with appropriate error message.
	*/
	function validateEmail($strData, &$strDescription)
	{
		if($strData != "")
		{
			if(!eregi('^' .
				'[a-z0-9]+([_.-][a-z0-9]+)*' .    //user
				'@' .
				'([a-z0-9]+([.-][a-z0-9]+)*)+' .   //domain
				'\.[a-z]{2,}' .                    //sld, tld
				'$', $strData)
				)
			{
				$strDescription = $strDescription."must consist of <br/>
								   an account, an at sign, and a <br/>
								   domain name with a dot in it, <br/>e.g. someone@yourcompany.com.<br/>";
				return(FALSE);
			}
		}else{
			$strDescription = $strDescription." shouldn't be blank.<br/>";
			return(FALSE);
		}

		return(TRUE);
	}

	/**
	* Function: validateURL
	* Input: STRING data, REFERENCE description
	* Output: BOOLEAN
	* Description: Returns TRUE if data looks like an URL.
	* $description set with appropriate error message.
	*/
	function validateURL($strData, &$strDescription)
	{
		if(!eregi('^http://', $strData))
		{
			$strDescription = "$strDescription $strData doesn't look right, please re-enter it.<br/>";
			return(FALSE);
		}

		return(TRUE);
	}
	/**
	 * 
	 * @param $strIP
	 * @param $strDescription
	 * @return bool
	 */
	function validateIP($strIP, &$strDescription)
	{
	    if(($strIP == "") || ($strIP == "..."))
	    {
	        $strDescription = $strDescription." is missing, please re-enter.";
	        return false;
	    }
		/*
		 * Quickest way to check ip is valid: 
		 * convert from ip to long, convert back from long to ip
		 * and then compare final result with original
		 */
		if((long2ip(ip2long($strIP)) != $strIP) && ($strIP != "localhost"))
		{
			if($strDescription == "")
				$strDescription = $strIP." is not a valid IP address, please re-enter it.";
			else 
				$strDescription = $strIP." is not a valid ".$strDescription;
			return(FALSE);
		}

		return(TRUE);
	}
	
	
	/**
	 * Validate that ip passed is a valid netmask
	 * @param $strNetmask
	 * @return bool
	 */
	function validateNetmask($strNetmask, &$strDescription)
	{
	    if(($strNetmask == "") || ($strNetmask == "..."))
	    {
	        $strDescription = "Netmask is missing, please re-enter.";
	        return false;
	    }
		$strSQL = "SELECT * 
    			   FROM tblvalid_netmask 
    			   WHERE valid_netmask = '".$strNetmask."'";
    	
    	$qryValidNetmask = new MySqlResults($strSQL);
    	if($qryValidNetmask->numrows == 0)
    	{
    		$strDescription = $strNetmask." is not a valid netmask, please re-enter it.";
    		return false;
    	}else
    		return true;
	}
	
	/*
	function validIP($ip){
    if(preg_match("^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}^", $ip))
        return true;
    else
        return false;
	} */

	/**
	* Function: validateAlpha
	* Input:	STRING data, REFERENCE description
	* Output: BOOLEAN
	* Description: Returns TRUE if data is composed of letter and spaces only. $description set
	* with appropriate error message.
	*/
	function validateAlphaNoSpace($strData, &$strDescription)
	{
		if(eregi('[^[:alpha:]]', $strData))
		{
			$strDescription .= ", please compose the text for this field using letters only.<br/>";
			return(FALSE);
		}

		return(TRUE);
	}
	/**
	* Function: validateAlpha
	* Input:	STRING data, REFERENCE description
	* Output: BOOLEAN
	* Description: Returns TRUE if data is composed of letter and spaces only. $description set
	* with appropriate error message.
	*/
	function validateAlpha($strData, &$strDescription)
	{
		if(eregi('[^[:alpha:][:space:]]', $strData))
		{
			$strDescription = "Please compose the text for '$strDescription' using letters and spaces only.<br/>";
			return(FALSE);
		}

		return(TRUE);
	}

	/**
	* Function: validateNumber
	* Input:	STRING $strData, REFERENCE $strDescription
	* Output: BOOLEAN
	* Description: Returns TRUE if data is composed of numbers only. $strDescription set
	* with appropriate error message.
	*/
	function validateNumber($strData, &$strDescription)
	{
	    if($strData == "")
	    {
	        $strDescription = $strDescription ." shouldn't be blank, please re-enter.<br/>";
	        return false;
	    }
		if(!is_numeric($strData))
		{
			$strDescription = $strDescription ." value should only be composed of numbers, please re-enter.<br/>";
			return(FALSE);
		}
		return(TRUE);
	}
	
	/**
	* Function: validatePhoneNumber
	* Input:	STRING $strData, REFERENCE $strDescription
	* Output: BOOLEAN
	* Description: Returns TRUE if data is composed of numbers and space only. $strDescription set
	* with appropriate error message. 
	*/
	function validatePhoneNumber($strData, &$strDescription)
	{
		if(eregi('[^[:digit:][:space:]]', trim($strData)))
		{
			$strDescription .= ", please compose the text for this field using numbers and spaces only.<br/>";
			return(FALSE);
		}

		return(TRUE);
	}

	/**
	* Function: validateAlpha
	* Input:	STRING data, REFERENCE description
	* Output: BOOLEAN
	* Description: Returns TRUE if data is composed of letter and spaces only. $description set
	* with appropriate error message.
	*/
	function validateAlphaNumeric($strData, &$strDescription)
	{
	    //Check that the user hasn't entered only spaces
        if(trim($strData) == "")
        {
            $strDescription .= " shouldn't be blank, please re-enter;<br/>";
			return(FALSE);
        }
		if((eregi('[^[:alpha:][:digit:]]', $strData))||($strData == ""))
		{
			$strDescription .= ", please compose the text for this field using<br/>letters and numbers only.<br/>";
			return(FALSE);
		}

		return(TRUE);
	}

    /**
	* Function: validateAlphaSpace
	* Input:	STRING data, REFERENCE description
	* Output: BOOLEAN
	* Description: Returns TRUE if data is composed of letters, numbers and spaces only. $description set
	* with appropriate error message.
	*/
	function validateAlphaNumericSpace($strData, &$strDescription)
	{
        //Check that the user hasn't entered only spaces
        if($strData == "")
        {
            $strDescription .= " shouldn't be blank, please re-enter;<br/>";
			return(FALSE);
        }
		if(str_replace(" ","",$strData) != "")
		{
			if(eregi('[^[:alpha:][:digit:][:space:]]', $strData))
			{
				$strDescription .= ", please compose the text for this field using<br/>letters, numbers and spaces only.<br/>";
				return(FALSE);
			}
		}else{
			$strDescription .= ", must contain alphanumeric characters as well as spaces<br/>";
			return(FALSE);
		}

		return(TRUE);
	}

	/**
	 * Validate $strData is in the range $intStart-$intEnd
	 *
	 * @param int $strData
	 * @param int $intStart
	 * @param int $intEnd
	 * @param string by reference $strDescription
	 * @return boolean
	 */
	function validateRange($strData, $intStart = "", $intEnd = "", &$strDescription)
	{
		$intRange 	= 0;
		$boResult 	= false;
			
		if( eregi('[^[:digit:]]', $strData) )
		{
			$strDescription .= ", please compose the text for this field using numbers only.<br/>";
		}else{

			//echo '<br/> validated as number<br/>';
			if(! eregi('[^[:digit:]]',$intStart))
				$intRange = $intRange + 1;			
			
			if(! eregi('[^[:digit:]]',$intEnd))
				$intRange = $intRange + 2;			

			//echo "<br/> switch ".$intRange."<br/>";
			
			switch ($intRange) {
				case 0:
					//Both = 0
					$strDescription .= ": please compose the range for these fields using numbers only.<br/>";
					break;
				case 1:
					//Only Start has value so do >= comparism
					if($intStart <= $strData)
					{
						$boResult = true;
					}else{
						$strDescription .= ": '$strData' is not >= $intStart, please re-enter data.<br/>";
					}
					break;
				case 2:
					//End has value so do <= than comparism
					if($intEnd >= $strData)
					{
						$boResult = true;
					}else{
						$strDescription .= ": '$strData' is not <= $intEnd, please re-enter data.<br/>";
					}
					break;
				case 3:
					//Both have values above 0  so check between
					if(($intStart <= $strData) && ($intEnd >= $strData))
					{
						$boResult = true;
					}else{
						$strDescription .= ": '$strData' is not in the range $intStart - $intEnd, please re-enter data.<br/>";
					}
					break;
			}
		}

		return($boResult);
	}
	
	function validateDomainARecord($strDomain, &$strDescription )
	{
		return validateDomain($strDomain , 'A', $strDescription);
	}
	
	function validateDomain($strDomain ,$strType = 'MX', &$strDescription)
	{
		$strType = strtoupper($strType);
	    if(!checkdnsrr($strDomain, $strType))
	    {
	    	/* Types A, MX, NS, SOA, PTR, CNAME, AAAA, A6, SRV, NAPTR, TXT or ANY */
	    	switch ($strType)
	    	{
	    		case "A":
	    			$strDescription .= " ".$strDomain.", does not have a valid A record.<br/>";
	    			break;
	    		case "MX":
	    			$strDescription .= " ".$strDomain.", does not have a valid MX record.<br/>";
	    			break;
	    			
	    		default: 
	    			$strDescription .= "DNS lookup failed, please check<br/>".$strDomain." is correct.";
	    	}
	    	
	    	return false;
	    }
	    
	    return true;	
	}
	
	function validatePasswordText($strPassword, &$strDescription, $iMinLength = 6, $iMaxLength = 15, $iMinUpper = 1, $iLower = 1, $iMinDigits = 2)
	{	
		if( (strlen($strPassword) > $iMinLength   // at least 8 chars 
			&& strlen($strPassword) < $iMaxLength // at most 16 chars 
			&& preg_match('`[A-Z]`',$strPassword) // at least one upper case 
			&& preg_match('`[a-z]`',$strPassword) // at least one lower case 
			&& preg_match('`[0-9]`',$strPassword) // at least one digit 
			) && ($strPassword != "P4ssw0rD"))
		{
			return true;
		}else{
			$strDescription = "Passwords must be $iMinLength - $iMaxLength characters long, containing a mixture of
								<br/> upper case, lower case characters and at least 1 number e.g P4ssw0rD (but not this)";
			return false;
		}
	}


?>
